package com.kfm.wiki.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kfm.wiki.entity.Ebook;
import com.kfm.wiki.mapper.EbookMapper;
import com.kfm.wiki.service.EbookService;
import com.kfm.wiki.util.PageReq;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 10854
* @description 针对表【ebook.http(电子书)】的数据库操作Service实现
* @createDate 2022-09-21 11:27:09
*/
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook>
    implements EbookService{

    @Override
    public Page<Ebook> page(Ebook ebook, PageReq page) {
        if (ObjectUtil.isEmpty(ebook)){
            return null;
        }
        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(ebook.getName())){
            wrapper.like("name", ebook.getName());
        }
        if (ObjectUtil.isNotEmpty(ebook.getCategory2Id())){
            wrapper.eq("category2_id", ebook.getCategory2Id());
        }
        if (ObjectUtil.isNotEmpty(ebook.getCategory1Id())){
            wrapper.eq("category1_id", ebook.getCategory1Id());
        }
        PageHelper.startPage(page.getPage(), page.getSize());
        Page<Ebook> ebooks = (Page<Ebook>) getBaseMapper().selectList(wrapper);
        return ebooks;
    }

    @Override
    public boolean save(Ebook ebook) {
        if (ObjectUtil.isEmpty(ebook.getId())){
            ebook.setId(IdUtil.getSnowflakeNextId());
            return getBaseMapper().insert(ebook) == 1;
        } else {
            return getBaseMapper().updateById(ebook) == 1;
        }
    }
}




